package cn.doitedu.dao;

import cn.doitedu.pojo.CombineCondition;
import cn.doitedu.pojo.EventCondition;
import cn.doitedu.pojo.EventStateBean;
import cn.doitedu.pojo.LogBean;
import org.apache.flink.api.common.state.ListState;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @create: 2021-11-16 22:39
 * @author: 静如海的男人
 * @program: eagle-text
 **/

public class HistoryEventStateDaoImpl implements HistoryEventDao {
    //状态里缓存这最近两小时的行为数据
    private ListState<EventStateBean> eventListState;

    public HistoryEventStateDaoImpl(ListState<EventStateBean> eventListState) {
        this.eventListState = eventListState;
    }

    @Override
    public String queryEventSequenceStr(LogBean bean, CombineCondition combineCondition, long startTime, long endTime) throws Exception {
        long setStartTime = startTime;
        long setEndTime = endTime;

        //事先定义好的行为规则
        List<String> targets = combineCondition.getEventConditions().stream().map(EventCondition::getEventId).collect(Collectors.toList());
        //将查询的数据映射成数字或字母
        StringBuffer sb = new StringBuffer();
        for (EventStateBean eventState : eventListState.get()) {
            long timeStamp = eventState.getTimeStamp();
            if (timeStamp >= setStartTime && timeStamp <= endTime) {
                int i = targets.indexOf(eventState);
                if (i >= 0) {
                    sb.append((char) (i + 65));
                } else {
                    sb.append(i + 1);
                }
            }
        }
        //返回映射的字符串
        return sb.toString();
    }
}
